package Solution.problem088.MergeSortedArray;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/06/18 09:48
 */
public class Solution {

    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        if (nums1 == null || nums1.length < n) {
            return;
        }
        int index = nums1.length - 1;
        while (m > 0 || n > 0) {
            if (m <= 0) {
                nums1[index] = nums2[n - 1];
                n--;
            } else if (n <= 0) {
                return;
            } else {
                int num1 = nums1[m - 1];
                int num2 = nums2[n - 1];
                if (num1 > num2) {
                    nums1[index] = num1;
                    m--;
                } else {
                    nums1[index] = num2;
                    n--;
                }
            }
            index--;
        }
    }


    public static void main(String[] args) {
        int[] nums1=new int[]{2,0};
        int[] nums2=new int[]{1};
        merge(nums1,1,nums2,1);
        System.out.println(nums1);
    }
}
